package com.dailyblue.project.estate.car.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.car.feign.OwnerFeign;
import com.dailyblue.project.estate.car.service.CarService;
import com.dailyblue.project.estate.commons.bean.Car;
import com.dailyblue.project.estate.commons.bean.Owner;
import com.dailyblue.project.estate.commons.mapper.CarMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Resource
    private CarMapper carMapper;
    @Resource
    private OwnerFeign ownerFeign;

    @Override
    public JsonResult addCar(Car car) {
        boolean b = save(car);
        if (!b) {
            // 抛异常
        }
        return ResultTool.success("success");
    }

    @Cacheable(cacheNames = "findAllCar")
    public JsonResult find() {
        List<Car> cars = list();
        cars.forEach(car -> {
            String ownerId = car.getOwnerId();
            JsonResult<Owner> owner = ownerFeign.getOwner(ownerId);
            car.setOwner(owner.getData());
        });
        return ResultTool.success(cars);
    }
}
